import {
    gethistorymessageapi
} from '../../api/message'
import user from '../../../utils/user.js'
const app = getApp(); //获取应用实例
Page({
    data: {
        messagelist: [],
        pageSize: 6,
        pageNum: 1,
        hasnext: false,
        toview: '',
        num: ''
    },
    preview(e) {
        wx.previewImage({
            urls: [e.currentTarget.dataset.img],
        })
    },
    onLoad(options) {
        let pageStack = getCurrentPages()
        console.log(pageStack, '1111111111111111111')
        user.waitLogin().then(() => {
            this.gethistorymessage()
        })
    },
    onShow() {

    },
    // 查询历史群发消息记录
    async gethistorymessage() {
        let res = await gethistorymessageapi({
            pageSize: this.data.pageSize,
            pageNum: this.data.pageNum
        })
        res.rows.reverse()
        res.rows.forEach((item, index) => {
            console.log(item, 'item')
            if (index == 0) {
                res.rows[index].flag = true
            } else {
                if (((new Date(`${res.rows[index].createTime.split(' ')[0]}`).getTime()) - (new Date(`${res.rows[index-1].createTime.split(' ')[0]}`).getTime())) >= 86400000) {
                    console.log('触发 true')
                    res.rows[index].flag = true
                } else if (((new Date(`${res.rows[index].createTime.split(' ')[0]}`).getTime()) - (new Date(`${res.rows[index-1].createTime.split(' ')[0]}`).getTime())) <= 86400000) {
                    res.rows[index].flag = false
                    console.log('触发 false')
                }
            }
        })
        console.log(res.rows, 'rows')
        this.setData({
            hasnext: Math.ceil(res.total / this.data.pageSize) <= this.data.pageNum ? false : true,
            messagelist: res.rows,
        })
        console.log(this.data.messagelist, 'messagelist')
        if (this.data.num == 0) {
            this.setData({
                num: 1,
                toview: 'item' + res.rows[res.rows.length - 1].id
            })
        }
    },
    // 跳转新建群发消息
    gonewgroupmessage() {
        wx.navigateTo({
            url: '/packageMassmessage/pages/new-group-message/new-group-message',
        })
    },
    golooksendingrange(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../look-sending-range/look-sending-range?id=' + id,
        })
    },
    // 页面上拉触底事件的处理函数
    async onScrolltolower() {
        return
        if (this.data.hasnext) {
            this.setData({
                pageNum: this.data.pageNum + 1
            })
            let res = await gethistorymessageapi({
                pageSize: this.data.pageSize,
                pageNum: this.data.pageNum
            })
            let arr = res.rows
            let carr = this.data.messagelist.concat(arr)
            this.setData({
                hasnext: Math.ceil(res.total / this.data.pageSize) <= this.data.pageNum ? false : true,
                messagelist: carr
            })
        } else {
            // wx.showToast({
            //     title: '暂无更多',
            //     icon: "none"
            // })
        }

    },
    // 到达底部加载上一页
    async onScrolltoupper() {
        if (this.data.hasnext) {
            this.setData({
                pageNum: this.data.pageNum + 1
            })
            let res = await gethistorymessageapi({
                pageSize: this.data.pageSize,
                pageNum: this.data.pageNum
            })
            // unshift
            res.rows.reverse()
            
            let arr = this.data.messagelist
            arr.unshift(...res.rows)
            arr.forEach((item, index) => {
                console.log(item, 'item')
                if (index == 0) {
                    arr[index].flag = true
                } else {
                    if (((new Date(`${arr[index].createTime.split(' ')[0]}`).getTime()) - (new Date(`${arr[index-1].createTime.split(' ')[0]}`).getTime())) >= 86400000) {
                        console.log('触发 true')
                        arr[index].flag = true
                    } else if (((new Date(`${arr[index].createTime.split(' ')[0]}`).getTime()) - (new Date(`${arr[index-1].createTime.split(' ')[0]}`).getTime())) <= 86400000) {
                        arr[index].flag = false
                        console.log('触发 false')
                    }
                }
            })
            // let carr = this.data.messagelist.concat(arr)
            this.setData({
                hasnext: Math.ceil(res.total / this.data.pageSize) <= this.data.pageNum ? false : true,
                messagelist: arr
            })
        }
    },
})